package com.company.cloud.mall.modules.app.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.art.modules.api.response.Artist;
import com.company.cloud.art.modules.api.service.ArtArtistRemoteService;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.mall.modules.app.controller.resp.CartItemModel;
import com.company.cloud.mall.modules.app.controller.resp.CartModel;
import com.company.cloud.mall.modules.app.service.ShopCartService;
import com.company.cloud.mall.modules.base.entity.CartInfo;
import com.company.cloud.mall.modules.base.service.CartInfoService;
import com.company.cloud.mall.modules.cache.models.FrameModel;
import com.company.cloud.mall.modules.cache.service.ShopOrderCacheService;
import com.company.cloud.product.modules.api.response.PrdSkuStock;
import com.company.cloud.product.modules.api.response.Sku;
import com.company.cloud.product.modules.api.service.ProductRemoteService;
import com.company.cloud.product.modules.api.service.ProductStoctRemoteService;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 购物车
 * */
@Service
public class ShopCartServiceImpl implements ShopCartService{

	@Autowired
	private CartInfoService cartInfoService;
	@Autowired
	private ProductRemoteService productRemoteService;
	@Autowired
	private ProductStoctRemoteService productStoctRemoteService;
	@Autowired
	private ShopOrderCacheService shopOrderCacheService;
	@Autowired
	private ArtArtistRemoteService artArtistRemoteService;
	
	@Override
	public List<CartModel> getUserCartList(Integer userId) {
		List<CartModel> data=new ArrayList<CartModel>();
		List<CartInfo> cartList= cartInfoService.list(new QueryWrapper<CartInfo>()
				.eq("user_id", userId).orderByDesc("company_no","product_no"));
		
		if(CollectionUtils.isEmpty(cartList)) {
			return data;
		}
		
		Set<String> companyNoSet=new HashSet<String>();
		Set<String> skuNoSet=new HashSet<String>();
		cartList.forEach(item->{
			companyNoSet.add(item.getCompanyNo());
			skuNoSet.add(item.getSkuNo());
		});
		//todo
		BaseClientResult<Map<String,Sku>> skuResp=productRemoteService.getSkuMapByNos(skuNoSet);
		Map<String,Sku>  productMap= BaseClientResult.getResultData(skuResp);
		
		BaseClientResult<Map<String, PrdSkuStock>> prdStockResp=productStoctRemoteService.getSkuStockList(skuNoSet);
		Map<String, PrdSkuStock> stockMap=BaseClientResult.getResultData(prdStockResp);
		Set<String> artistNos=new HashSet<String>();
		
		Map<String, FrameModel> frameMap= shopOrderCacheService.getFrameMap();
		
		for(String companyNo:companyNoSet) {
			CartModel cart=new CartModel();
			cart.setCompanyNo(companyNo);
			cartList.forEach(item->{
				if(companyNo.equals(item.getCompanyNo())) {
					CartItemModel cartItem=new CartItemModel(item);
					Sku sku=productMap.get(item.getSkuNo());
					if(sku!=null) {
						if(StrUtil.isNotEmpty(sku.getBizValue4())) {
							Set<String> artistNos2= StrSplitUtils.parse2set(sku.getBizValue4(), String.class);
							artistNos.addAll(artistNos2);
						}
						
						cartItem.setProductName(sku.getName());
						cartItem.setProductCaption(sku.getCaption());
						cartItem.setProductImage(sku.getImage());
						cartItem.setPrice(sku.getPrice());
						cartItem.setFreightFee(sku.getFreightFee());
						cartItem.setSkuItems(sku.getItems());
						cartItem.setBizValue1(sku.getBizValue1());
						cartItem.setBizValue2(sku.getBizValue2());
						cartItem.setBizValue3(sku.getBizValue3());
						cartItem.setBizValue4(sku.getBizValue4());
						cartItem.setBizValue5(sku.getBizValue5());
						cartItem.setBizValue6(sku.getBizValue6());
						cartItem.setBizValue7(sku.getBizValue7());
						cartItem.setBizValue8(sku.getBizValue8());
						cartItem.setBizValue9(sku.getBizValue9());
						cartItem.setBizValue10(sku.getBizValue10());
						cartItem.setBizValue11(sku.getBizValue11());
						cartItem.setBizValue12(sku.getBizValue12());
						cartItem.setBizValue13(sku.getBizValue13());
						cartItem.setBizValue14(sku.getBizValue14());
						cartItem.setBizValue15(sku.getBizValue15());
						cartItem.setBizValue16(sku.getBizValue16());
						cartItem.setBizValue17(sku.getBizValue17());
						cartItem.setBizValue18(sku.getBizValue18());
						cartItem.setBizValue19(sku.getBizValue19());
						cartItem.setBizValue20(sku.getBizValue20());
					}
					if(!CollectionUtils.isEmpty(stockMap)) {
						PrdSkuStock skuStock= stockMap.get(item.getSkuNo());
						if(skuStock!=null) {
							cartItem.setStockNum(skuStock.getNum());
							cartItem.setStockFreezeNum(skuStock.getFreezeNum());
						}
					}
					if(StrUtil.isNotEmpty(item.getFrameNo())) {
						FrameModel frame= frameMap.get(item.getFrameNo());
						if(frame!=null) {
							cartItem.setFrameName(frame.getName());
							cartItem.setFramePrice(frame.getPrice());
						}
					}
					
					cart.addItem(cartItem);
				}
			});
			data.add(cart);
		}
		
		//作家
		BaseClientResult<Map<String, Artist>> artistResp=artArtistRemoteService.artistInfoMap(artistNos);
		Map<String, Artist> artistData =BaseClientResult.getResultData(artistResp, new HashMap<String,Artist>());
		
		data.forEach(item->{
			if(CollUtil.isNotEmpty(item.getItems())) {
				for(CartItemModel cim:item.getItems()) {
					if(StrUtil.isNotEmpty(cim.getBizValue4())) {
						Set<String> artistNos3= StrSplitUtils.parse2set(cim.getBizValue4(), String.class);
						if(artistNos3.size()>0) {
							for(String ano:artistNos3) {
								Artist  artist= artistData.get(ano);
								if(artist!=null) {
									cim.addArtist(this.getArtist(artist));
								}
							}
						}
					}
				}
			}
		});
		
		return data;
	}
	

	private Map<String, Object> getArtist(Artist  artist) {
		Map<String, Object> data=new HashMap<String, Object>();
		data.put("artistNo", artist.getArtistNo());
		data.put("name", artist.getName());
		data.put("nameFirstPy", artist.getNameFirstPy());
		data.put("headImg", artist.getHeadImg());
		data.put("prdNum", artist.getPrdNum());
		return data;
	}
	
}
